import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Solution {
    public static void main(String[] args) {
       String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
       List<List<String>> list = groupAnagrams(strs);
        System.out.println(list);
    }
    //字母异位词分组
    public static List<List<String>> groupAnagrams(String[] strs) {
        int n = strs.length;
        HashMap<String,List<String>> map = new HashMap<>();
        for(int i = 0; i < n; i++) {
            char[] arr = strs[i].toCharArray();
            Arrays.sort(arr);
            String s = new String(arr);
            List list = map.getOrDefault(s,new ArrayList<>());
            list.add(strs[i]);
            map.put(s,list);
        }
        return new ArrayList<>(map.values());
    }

}
